package org.epidroid.epidroid.androsoul.ui.contactlist;

import org.epidroid.epidroid.R;
import org.epidroid.epidroid.androsoul.engine.AndroSoul;
import org.epidroid.epidroid.androsoul.ui.AndroSoulActivity;

import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

public class LoginForm {

	private AndroSoulActivity _activity = null;

	private LinearLayout _layout = null;
	private EditText _editLogin = null;
	private EditText _editPassword = null;
	private Button _loginButton = null;
	//private TextView _infoTxt = null;

	SharedPreferences _prefs = null;

	public LoginForm(AndroSoulActivity activity) {
		_activity = activity;
		_layout = (LinearLayout) _activity.findViewById(R.id.LoginForm);
		_loginButton = (Button) _activity.findViewById(R.id.buttonLogin);
		_editLogin = (EditText) _activity.findViewById(R.id.editLogin);
		_editPassword = (EditText) _activity.findViewById(R.id.editPassword);

		_prefs = PreferenceManager.getDefaultSharedPreferences(_activity);

		_loginButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {

				if (_editLogin.getText().toString().equals("")
						|| _editLogin.getText().toString().equals("")) {
					_activity.showToast(_activity.getString(R.string.credentials_provide));
					return;
				}

				_loginButton.setClickable(false);

				SharedPreferences.Editor editor = _prefs.edit();
				editor.putString("login", _editLogin.getText().toString());
				editor.putString("pass", _editPassword.getText().toString());
				editor.commit();

				_activity.switchLogin();
			}
		});
		show();
	}

	public void show() {

		_editLogin.setText(_prefs.getString("login", ""));
		_editPassword.setText(_prefs.getString("pass", ""));

		_loginButton.setClickable(true);
		_layout.setVisibility(View.VISIBLE);
	}

	public void hide() {
		_layout.setVisibility(View.GONE);
		Log.v(AndroSoul.LOGCAT, "hide login form");
	}

	public void showInfo(String info) {

	}

}
